import Api from "./Api";

const LIMIT = 5;

const OFFSET = (page) => {
  return (page - 1) * LIMIT;
};

export default {
  //单个文章
  create: (article) => Api.post("/articles", { article }),
  get: (slug) => Api.get("/articles/" + slug),
  update: (article) => Api.put("/articles/" + article.slug, { article }),
  delete: (slug) => Api.delete("/articles/" + slug),

  //喜欢文章
  favorite: (slug) => Api.post("/favorites/" + slug),
  unfavorite: (slug) => Api.delete("/favorites/" + slug),

  getAuthor: (author, page) =>
    Api.get(`/articles?author=${author}&limit=${LIMIT}&offset=${OFFSET(page)}`),
  //作者喜欢的文章
  getFavorite: (author, page) =>
    Api.get(
      `/articles?favorite=${author}&limit=${LIMIT}&offset=${OFFSET(page)}`
    ),
};
